<%--
  Created by IntelliJ IDEA.
  User: Tsuki
  Date: 2023-07-16
  Time: 13:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib uri="http://wego.com/format" prefix="f" %>
<html>
<head>
    <title>品类添加更新</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/showBigImage.js" charset="utf-8"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container ">
    <div class="layuimini-main">
        <form class="layui-form layui-row layui-col-space16" action="">
            <%--品类编号--%>
            <input type="text" name="id" autocomplete="off" value="${category.id}" hidden>

            <c:if test="${addtypes != 0}">
                <%--父级品类:--%>
                <div class="layui-form-item">
                    <label class="layui-form-label">父级品类:</label>
                    <div class="layui-input-block">
                        <select id="category1" lay-filter="cateGoryName1" name="pid" class="cateGoryName" lay-affix="clear">
                            <option value="-1">一级品类</option>
                        </select>
                    </div>
                </div>
            </c:if>

            <%--商品名称--%>
            <div class="layui-form-item">
                <label class="layui-form-label">品类名称:</label>
                <div class="layui-input-block">
                    <input type="text" lay-verify="required" name="name"
                           autocomplete="off" class="layui-input" value="${category.name}" placeholder="请输入品类名称">
                </div>
            </div>

            <%--商品状态--%>
            <div class="layui-form-item">
                <label class="layui-form-label">品类状态:</label>
                <div class="layui-input-block">
                    <c:if test="${category.state==1}">
                        <input type="radio" lay-verify="required" value="1" name="state" lay-skin="tag" title="上架"
                               autocomplete="off" checked>
                        <input type="radio" lay-verify="required" value="0" name="state" lay-skin="tag" title="下架"
                               autocomplete="off">
                    </c:if>

                    <c:if test="${category.state==0}">
                        <input type="radio" lay-verify="required" value="1" name="state" lay-skin="tag" title="上架"
                               autocomplete="off">
                        <input type="radio" lay-verify="required" value="0" name="state" lay-skin="tag" title="下架"
                               autocomplete="off" checked>
                    </c:if>

                    <c:if test="${category.state==null}">
                        <input type="radio" name="state" value="1" lay-skin="tag" title="上架"
                               autocomplete="off">
                        <input type="radio" name="state" value="0" lay-skin="tag" title="下架"
                               autocomplete="off">
                    </c:if>

                </div>
            </div>

            <%--优先级--%>
            <div class="layui-form-item">
                <label class="layui-form-label">优先级:</label>
                <div class="layui-input-block">
                    <input type="number" name="priority"
                           autocomplete="off" class="layui-input" value="${category.priority}" lay-verify="required|number"
                           placeholder="请输入品类展示优先级">
                </div>
            </div>

            <%--商品简介--%>
            <div class="layui-form-item">
                <label class="layui-form-label">品类信息:</label>
                <div class="layui-input-block">
                    <textarea class="layui-textarea" placeholder="请输入商品简介" name="info">${category.info}</textarea>
                </div>
            </div>

            <%--操作按钮--%>
            <div class="layui-form-item" style="padding-left: 1100px">
                <div class="layui-btn-container ">
                    <button type="reset" class="layui-btn layui-btn-normal layui-row-xs">
                        清空
                    </button>
                    <button id="goods_submit" class="layui-btn layui-btn-danger layui-row-xs" lay-submit
                            lay-filter="goodsSubmit"> 提交
                    </button>
                </div>
            </div>

        </form>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'element', 'layer'], function (message) {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        element = layui.element;

        /**
         * 加载一级类别选择框内容
         */
        $.ajax({
            url: '${pageContext.request.contextPath}/manager/category/list_id_name',
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                // 动态添加新的选项
                $.each(data, function (index, item) {
                    var option = $('<option></option>').val(item.id).text(item.name);
                    $('.cateGoryName').append(option);
                });
                form.render();
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('获取数据失败');
            }
        });

        /**
         * 表单提交事件
         */
        form.on('submit(goodsSubmit)', function (data) {
            var formData = data.field; // 获取表单数据
            $.ajax({
                url: '${pageContext.request.contextPath}/manager/category/addUpdate',
                type: 'POST',
                data: formData,
                dataType: 'json',
                success: function (result) {
                    var index = parent.layer.getFrameIndex(window.name); // 获取当前 iframe 层的索引
                    parent.layer.close(index); // 关闭当前 iframe 弹层
                },
                error: function () {
                }
            });
            return false;
        });


    });
</script>
</body>
</html>
